﻿//----------------------------------------------------------------------------------------------------
// A NodeInstance is the representation of a node in a level
//----------------------------------------------------------------------------------------------------

package HG.Visual
{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.Timer;
	
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	
	public class VisualTextProgress extends MovieClip
	{
		var m_targetProgress:Number;
		var m_currentProgress:Number;
		
		var m_incrementTimer:Timer;
		var m_animTimer:Timer;
		
		//----------------------------------------------------------------------------------------------------
		public function VisualTextProgress()
		{
			m_incrementTimer = new Timer(25);
			m_incrementTimer.addEventListener(TimerEvent.TIMER, UpdateProgress);
			
			m_animTimer = new Timer(0);
			m_animTimer.addEventListener(TimerEvent.TIMER, Start);
			
			this.addEventListener(MouseEvent.CLICK, SkipAnimation);
			
			this.TXT_Value.text = "0%";
			this.scaleX = 0.25;
			this.scaleY = 0.25;
		}
		

		//----------------------------------------------------------------------------------------------------
		public function SetValue(val:Number, delay:Number)
		{
			m_incrementTimer.stop();
			m_targetProgress = val;
			m_currentProgress = 0;
			
			if (delay != 0)
			{
				m_animTimer.reset();
				m_animTimer.delay = delay;
				m_animTimer.start();
			}
			else
			{
				m_currentProgress = val;
				this.TXT_Value.text = val + "%";
				
				var scale:Number = 0.5 + (m_currentProgress / 200);
			
				new Tween(this, "scaleX", Elastic.easeOut, this.scaleX, scale, 0.5, true);
				new Tween(this, "scaleY", Elastic.easeOut, this.scaleY, scale, 0.5, true);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		public function Start(evt:TimerEvent)
		{
			m_animTimer.stop();
			m_incrementTimer.start();
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SkipAnimation(evt:MouseEvent)
		{
			SetValue(m_targetProgress, 0);
		}
		
		//----------------------------------------------------------------------------------------------------
		private function UpdateProgress(evt:TimerEvent)
		{
			var diff:Number = m_targetProgress - m_currentProgress;
			
			//m_currentProgress += Math.round(diff / 6);
			m_currentProgress++;
			
			if (m_currentProgress >= (m_targetProgress - 2))
			{
				m_incrementTimer.stop();
				m_currentProgress = m_targetProgress;
			}
			
			this.TXT_Value.text = m_currentProgress + "%";
			
			var scale:Number = 0.5 + (m_currentProgress / 200);
			
			//this.scaleX = scale;
			//this.scaleY = scale;
			
			new Tween(this, "scaleX", Elastic.easeOut, this.scaleX, scale, 0.5, true);
			new Tween(this, "scaleY", Elastic.easeOut, this.scaleY, scale, 0.5, true);
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}